package top.chmin.project.cloud.sys.index.info;
import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.Page;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import top.chmin.project.cloud.sys.dict.data.DictData;
import top.chmin.project.cloud.sys.dict.data.DictDataCondition;



/**
 * chmin
 * 2024-01-26 13:55:57
 * cloud-code
 */
@Service
@Transactional
public class IndexInfoService {

    @Resource
    IndexInfoPlugin plugin;

    public int save(IndexInfoSaveRest.P p) {
        plugin.checkSave(p);
        IndexInfo indexInfo = BeanUtil.toBean(p, IndexInfo.class);
        plugin.beforeSave(indexInfo);
        int result = indexInfo.forInsert().execute();
        plugin.afterSave(p, indexInfo, result);
        return result;
    }

    public IndexInfoSelectRest.R select(IndexInfoSelectRest.P p) {
        plugin.checkSelect(p);
        IndexInfoCondition c = IndexInfoCondition.forSelect().id().eq(p.getId());
        plugin.beforeSelect(p, c);
        IndexInfoSelectRest.R r = c.selectFirst(IndexInfoSelectRest.R.class);
        plugin.afterSelect(p, r);
        return r;
    }

    public int update(IndexInfoUpdateRest.P p) {
        plugin.checkUpdate(p);
        IndexInfo indexInfo = BeanUtil.toBean(p, IndexInfo.class);
        plugin.beforeUpdate(indexInfo);
        int result = indexInfo.forUpdate().id().eq().execute();
        plugin.afterUpdate(p, indexInfo, result);
        return result;
    }

    public int delete(IndexInfoDeleteRest.P p) {
        plugin.checkDelete(p);
        IndexInfo indexInfo = BeanUtil.toBean(p, IndexInfo.class);
        plugin.beforeDelete(indexInfo);
        int result = indexInfo.forDelete().id().eq().execute();
        plugin.afterDelete(p, indexInfo, result);
        return result;
    }

    public Page<IndexInfoPageRest.R> page(IndexInfoPageRest.P p) {
        plugin.checkPage(p);
        IndexInfoCondition c = IndexInfoCondition.forSelect();
        c = p.isDesc() ? c.id().desc() : c.id().asc();
        plugin.beforePage(p, c);
        Page<IndexInfoPageRest.R> r = c.selectPage(IndexInfoPageRest.R.class);
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceRelateAppender$ */
        /*###########auto#start###########*/
        List<DictData> indexUniqueDictList = DictDataCondition.forSelect().typeCode().eq("000401").selectList();
        Map<String, DictData> indexUniqueDictMap = indexUniqueDictList.stream().collect(Collectors.toMap(DictData::getTypeValue, t -> t));
        r.getList().forEach(t -> t.setIndexUniqueText(indexUniqueDictMap.get(t.getIndexUnique()).getTypeLabel()));

        /*###########auto##end############*/
        /* append $serviceDictAppender$ */
        plugin.afterPage(p, r);
        return r;
    }

    /*###########auto#start###########*/
    public List<IndexInfoSelectIndexCodeOptionRest.R> selectIndexCodeOption(IndexInfoSelectIndexCodeOptionRest.P p) {
        plugin.checkSelectIndexCodeOption(p);
        IndexInfoCondition c = IndexInfoCondition.forSelect();
        c.limit(100);
        plugin.beforeSelectIndexCodeOption(p, c);
        List<IndexInfoSelectIndexCodeOptionRest.R> r = c.selectList(IndexInfoSelectIndexCodeOptionRest.R.class);
        plugin.afterSelectIndexCodeOption(p, r);
        return r;
    }

    /*###########auto##end############*/
    /* append $serviceSelectOptionMethodAppender$ */
}
